package com.aspose.cells;

import java.util.Iterator;

class zbwp {
   private Workbook a;
   private WorksheetCollection b;
   private zbwd c;
   private zbwc[] d;
   private int e;
   private zqz f;
   private zen[] g;

   public zbwp(Workbook var1, SaveOptions var2) throws Exception {
      this.a = var1;
      this.b = var1.getWorksheets();
      zsu.b(this.b);
      this.b.p();
      this.c = new zbwd(var1, var2);
      this.e = this.b.getCount();
      this.d = new zbwc[this.e];

      for(int var3 = 0; var3 < this.e; ++var3) {
         Worksheet var4 = this.b.get(var3);
         if (var4.getType() != 2) {
            this.d[var3] = new zbwc(this.c, var3);
         }
      }

   }

   void a(com.aspose.cells.b.a.d.zm var1) throws Exception {
      boolean var2 = this.b.f() == null;
      String var3;
      if (this.c.c != null) {
         var3 = this.c.c + "_Wb.xls";
         com.aspose.cells.b.a.d.ze var4 = com.aspose.cells.b.a.d.zd.a(var3);

         try {
            this.b(var4);
            this.b.f().a(var1);
            this.a(var2);
         } finally {
            if (var4 != null) {
               var4.a();
            }

         }

         com.aspose.cells.b.a.d.zd.d(var3);
      } else {
         com.aspose.cells.b.a.d.zh var8 = new com.aspose.cells.b.a.d.zh();
         this.b(var8);
         this.b.f().a(var1);
         this.a(var2);
         var8.a();
         var3 = null;
      }

   }

   private void b(com.aspose.cells.b.a.d.zm var1) throws Exception {
      zqa var2 = new zqa(var1);

      int var3;
      for(var3 = 0; var3 < this.e; ++var3) {
         this.b.get(var3).f();
      }

      if (this.b.f() == null) {
         this.b.a(new zvh(WorksheetCollection.h));
      }

      if (this.b.l != null) {
         this.b.l.a(this.b.f());
      }

      this.a();
      this.a(var2);
      this.b.f().a().a((Object)"Workbook", (Object)var1);
      com.aspose.cells.b.a.d.zh var12;
      if (this.b.getXmlMaps().getCount() > 0 && this.b.f().a().a("XML") == null) {
         var12 = new com.aspose.cells.b.a.d.zh();
         zcfx var4 = new zcfx(var12, Encoding.getUTF8());
         this.b.getXmlMaps().d = "";
         zuh var5 = new zuh(this.b.n());
         var5.a(var4);
         var4.e();
         this.b.f().a().a("XML", var12);
      }

      com.aspose.cells.b.a.d.zh var8;
      int var15;
      if (this.b.V() != null) {
         zaul var13 = this.b.V();
         if (var13.getCount() > 0) {
            zane var14 = this.b.f().a().b("_SX_DB_CUR");
            if (var14 == null) {
               var14 = new zane(new com.aspose.cells.b.a.zh("00000000-0000-0000-0000-000000000000"));
               this.b.f().a().a("_SX_DB_CUR", var14);
            }

            var15 = var14.b();
            Iterator var6 = var13.iterator();

            while(var6.hasNext()) {
               zauk var7 = (zauk)var6.next();
               var8 = new com.aspose.cells.b.a.d.zh();
               var7.a(var8);
               String var9 = com.aspose.cells.a.c.zp.d(var7.r);
               if (!var14.a((Object)var9)) {
                  var14.a(var9, var8);
               }
            }
         }
      }

      if (this.b.n().getSettings().getShared()) {
         var12 = this.b.f().a().a("User Names");
         if (var12 == null) {
            com.aspose.cells.b.a.d.zh var16 = new com.aspose.cells.b.a.d.zh(com.aspose.cells.a.c.zv.a());
            zvh var17 = new zvh(var16);
            var12 = var17.a().a("User Names");
            this.b.f().a().b("User Names", var12);
            var12 = var17.a().a("Revision Log");
            this.b.f().a().b("Revision Log", var12);
         }
      } else {
         this.b.f().a().g("User Names");
      }

      for(var3 = 0; var3 < this.e; ++var3) {
         Worksheet var18 = this.b.get(var3);
         if (var18.k() != null && !var18.getOleObjects().a() && var18.getOleObjects().getCount() > 0) {
            if (this.b.f().a().a("\u0001CompObj") == null) {
               byte[] var21 = com.aspose.cells.a.c.zv.c();
               com.aspose.cells.b.a.d.zh var23 = new com.aspose.cells.b.a.d.zh();
               var23.b(var21, 0, var21.length);
               this.b.f().a().a("\u0001CompObj", var23);
            }

            for(var15 = 0; var15 < var18.getOleObjects().getCount(); ++var15) {
               OleObject var24 = var18.getOleObjects().get(var15);
               if (!var24.a() && var24.getObjectData() != null) {
                  String var26 = "MBD" + com.aspose.cells.a.c.zp.e(var24.s());
                  if (var24.l()) {
                     com.aspose.cells.b.a.zh var27 = var24.t();
                     zane var29 = new zane(var27);
                     com.aspose.cells.b.a.d.zh var10 = new com.aspose.cells.b.a.d.zh(var24.j());
                     var29.a("\u0001Ole10Native", var10);
                     this.b.f().a().a(var26, var29);
                  } else {
                     zane var28;
                     com.aspose.cells.b.a.d.zh var30;
                     if (var24.g()) {
                        var28 = new zane(var24.t());
                        var30 = new com.aspose.cells.b.a.d.zh(var24.getObjectData());
                        var28.a("Package", var30);
                        this.b.f().a().a(var26, var28);
                     } else if (zakq.a(var24.getObjectData())) {
                        var8 = new com.aspose.cells.b.a.d.zh(var24.getObjectData());
                        zvh var31 = new zvh(var8);
                        zane var32 = var31.a();
                        var32.a(var24.t());
                        if (var24.getFileFormatType() == 33 && var32.b("MAPIMessage") == null) {
                           zane var11 = new zane(var24.t());
                           var11.a("MAPIMessage", var32);
                           var32 = var11;
                        }

                        this.b.f().a().a(var26, var32);
                     } else {
                        var28 = new zane(var24.t());
                        var30 = new com.aspose.cells.b.a.d.zh(var24.getObjectData());
                        var28.a("CONTENTS", var30);
                        this.b.f().a().a(var26, var28);
                     }
                  }
               }
            }
         }
      }

      if (this.b.E() != null || this.b.F() != null || this.c.e != null) {
         String var19 = null;
         if (this.b.getBuiltInDocumentProperties().contains("Version")) {
            var19 = this.b.getBuiltInDocumentProperties().getVersion();
            String[] var20 = com.aspose.cells.b.a.zv.d(this.b.getBuiltInDocumentProperties().getVersion(), '.');
            if (var20.length == 2 && zapb.c(var20[0]) && zapb.c(var20[1])) {
               var15 = com.aspose.cells.b.a.zo.a(var20[0]) << 16 | com.aspose.cells.b.a.zo.a(var20[1]);
               this.b.getBuiltInDocumentProperties().get("Version").setValue(var15);
            }
         }

         zrp var22 = new zrp();
         var22.a(this.b.getBuiltInDocumentProperties(), this.b.getCustomDocumentProperties(), this.b.f().a());
         if (var19 != null) {
            this.b.getBuiltInDocumentProperties().get("Version").setValue(var19);
         }
      }

      if (this.c.f != null) {
         this.a(var1, this.b.f(), this.c.f);
      } else if (this.c.e != null) {
         this.a(var1, this.b.f(), this.c.e);
         this.c.e = null;
      } else if (this.a.getSettings().getPassword() != null && !"".equals(this.a.getSettings().getPassword())) {
         zbw var25 = new zbw(this.a.getSettings().getPassword());
         this.a(var1, this.b.f(), var25);
      }

   }

   private void a() throws Exception {
      if (this.a.g != null && this.a.g.getCount() > 0 && this.a.f == null) {
         this.a.f = new ContentTypePropertyCollection();
         zaiv var1 = new zaiv(this.a);
         var1.a();
      }

      if (this.a.g != null && this.a.g.getCount() > 0 || this.a.f != null && this.a.f.getCount() > 0) {
         zte var2 = new zte(this.a, this.b.f());
         var2.a();
      }

   }

   private void a(zqa var1) throws Exception {
      this.b();
      this.c();
      long var2 = var1.b();
      int[] var4 = new int[this.e];
      (new zbx(this.a, this.c)).a(var1, var4);
      if (this.b.getActiveSheetIndex() >= this.e) {
         this.b.c(0);
      }

      Worksheet var5 = this.b.get(this.b.getActiveSheetIndex());
      var5.setSelected(true);

      for(int var6 = 0; var6 < this.e; ++var6) {
         this.c.a.e.l();
         long var7 = var1.b();
         var1.b((long)var4[var6]);
         var1.a(var7);
         var1.b(var7);
         Worksheet var9 = this.b.get(var6);
         if (var9.getType() == 2) {
            zjp var10 = new zjp(this.b, var6, var9.getCharts(), var1);
            var10.a(var9.getCharts().get(0));
         } else {
            zbwq var11 = new zbwq(var9, this.d[var6]);
            var11.a(var1);
            this.d[var6] = null;
         }
      }

   }

   private void b() {
      for(int var1 = 0; var1 < this.e; ++var1) {
         Worksheet var2 = this.b.get(var1);
         if (var2.getValidations() != null && var2.getValidations().getCount() != 0) {
            int var3 = 0;
            int var4 = 0;

            int[] var8;
            for(Iterator var5 = var2.getValidations().iterator(); var5.hasNext(); var4 = var8[0]) {
               Validation var6 = (Validation)var5.next();
               int[] var7 = new int[]{var3};
               var8 = new int[]{var4};
               var6.a(var7, var8);
               var3 = var7[0];
            }
         }
      }

   }

   private void c() {
      if (this.b.R() != null) {
         this.f = new zqz(this.b.Q());
         this.f.a(this.b.Q().d());
      }

      this.g = new zen[this.e];

      for(int var1 = 0; var1 < this.e; ++var1) {
         Worksheet var2 = this.b.get(var1);
         this.g[var1] = new zen();
         if (var2.t() != null) {
            this.g[var1].a = new zqx(0);
            this.g[var1].a.a(var2.getShapes().c().c());
         }

         int var3 = var2.hasAutofilter() ? var2.getAutoFilter().e() : 0;
         int var4 = var2.getValidations().c() ? 1 : 0;
         int var5 = 0;
         PivotFieldCollection var8;
         if (var2.b != null && var2.b.getCount() > 0) {
            for(Iterator var6 = var2.b.iterator(); var6.hasNext(); var5 += var8.getCount()) {
               PivotTable var7 = (PivotTable)var6.next();
               var8 = var7.getPageFields();
            }
         }

         int var11 = var3 + var4 + var5;
         if (var11 != 0) {
            this.g[var1].b = var11;
            if (var2.t() != null) {
               int var12 = this.b.Q().d().a(var2.getShapes().c().c().a() & '\uffff', var11);
               if (var12 != -1) {
                  var2.getShapes().c().c().b(var12);
               }
            }

            ComboBox var13;
            if (var2.hasAutofilter()) {
               var13 = new ComboBox(var2.getShapes(), var2.getAutoFilter());
               var2.getShapes().c(var13);
               int var14 = var2.getAutoFilter().e();
               var2.getShapes().b(var2.getShapes().e() + (var14 - 1));
               var2.getShapes().c().c().b(var2.getShapes().c().c().c() + (var14 - 1));
               var2.getShapes().c().c().a(var2.getShapes().c().c().b() + (var14 - 1));
               this.b.Q().d().b(var2.getShapes().c().c().a() & '\uffff', var14 - 1);
            }

            if (var2.getValidations().c()) {
               var13 = new ComboBox(var2.getShapes(), var2.getValidations());
               var2.getShapes().c(var13);
               var2.getValidations().a(var13.K());
            }

            if (var2.b != null && var2.b.getCount() > 0) {
               Iterator var15 = var2.b.iterator();

               while(var15.hasNext()) {
                  PivotTable var16 = (PivotTable)var15.next();
                  PivotFieldCollection var9 = var16.getPageFields();
                  if (var9.getCount() != 0) {
                     ComboBox var10 = new ComboBox(var2.getShapes(), var16);
                     var2.getShapes().c(var10);
                     var9.d = var10.K();
                     var2.getShapes().b(var2.getShapes().e() + (var9.getCount() - 1));
                     var2.getShapes().c().c().b(var2.getShapes().c().c().c() + (var9.getCount() - 1));
                     var2.getShapes().c().c().a(var2.getShapes().c().c().b() + (var9.getCount() - 1));
                     this.b.Q().d().b(var2.getShapes().c().c().a() & '\uffff', var9.getCount() - 1);
                  }
               }
            }
         }
      }

   }

   private void a(boolean var1) {
      if (this.b.n().f != null && this.b.n().f.getCount() > 0) {
         this.b.f().a().g("MsoDataStore");
         Iterator var2 = this.b.n().f.iterator();

         while(var2.hasNext()) {
            ContentTypeProperty var3 = (ContentTypeProperty)var2.next();
            this.b.getCustomDocumentProperties().remove(var3.getName());
         }
      }

      if (var1) {
         this.b.a((zvh)null);
      } else {
         this.b.f().a().g("_VBA_PROJECT_CUR");
         this.b.f().a().g("Workbook");
         if (this.b.E() != null) {
            this.b.f().a().g("\u0005SummaryInformation");
            this.b.f().a().g("\u0005DocumentSummaryInformation");
         }

         if (this.b.V() != null) {
            zaul var8 = this.b.V();
            if (var8.getCount() > 0) {
               zane var10 = this.b.f().a().b("_SX_DB_CUR");
               Iterator var4 = var8.iterator();

               while(var4.hasNext()) {
                  zauk var5 = (zauk)var4.next();
                  String var6 = com.aspose.cells.a.c.zp.d(var5.r);
                  var10.g(var6);
               }
            }
         }

         for(int var9 = 0; var9 < this.e; ++var9) {
            Worksheet var11 = this.b.get(var9);
            ShapeCollection var12 = var11.t();
            if (var12 != null) {
               zen var13 = this.g[var9];
               if (var13 != null) {
                  var12.b(var12.e() - var13.b);
                  if (var13.a == null) {
                     var11.a((ShapeCollection)null);
                  } else {
                     var12.c().c().a(var13.a);
                  }
               }

               int var14 = 0;

               while(var14 < var12.getCount()) {
                  switch(var12.get(var14).getMsoDrawingType()) {
                  case 24:
                     OleObject var7 = (OleObject)var12.get(var14);
                     if (!var7.a()) {
                        this.b.f().a().g("MBD" + com.aspose.cells.a.c.zp.e(var7.s()));
                     }
                  default:
                     ++var14;
                  }
               }
            }
         }

         if (this.f != null) {
            this.b.Q().d().a(this.f);
         } else {
            this.b.a((zra)null);
         }

      }
   }

   private void a(com.aspose.cells.b.a.d.zm var1, zvh var2, zbw var3) throws Exception {
      zsh var4 = new zsh();
      long var5 = var1.i();
      var4.b(var1, var3);
      var1.b(var5);
      zane var7 = var2.a().b("_SX_DB_CUR");
      if (var7 != null) {
         for(int var8 = 0; var8 < var7.b(); ++var8) {
            com.aspose.cells.b.a.d.zh var9 = (com.aspose.cells.b.a.d.zh)var7.b(var8);
            var4.b(var9, var3);
            var9.b(0L);
         }

      }
   }

   private void a(com.aspose.cells.b.a.d.zm var1, zvh var2, zsg var3) throws Exception {
      zsh var4 = new zsh();
      long var5 = var1.i();
      var4.a(var1, var3);
      var1.b(var5);
      zane var7 = var2.a().b("_SX_DB_CUR");
      if (var7 != null) {
         for(int var8 = 0; var8 < var7.b(); ++var8) {
            com.aspose.cells.b.a.d.zh var9 = (com.aspose.cells.b.a.d.zh)var7.b(var8);
            var4.a(var9, (zsg)var3);
            var9.b(0L);
         }

      }
   }

   private void a(com.aspose.cells.b.a.d.zm var1, zvh var2, zby var3) throws Exception {
      zsh var4 = new zsh();
      long var5 = var1.i();
      var4.a(var1, var3);
      var1.b(var5);
      zse.a(var2, var3);
      zane var7 = var2.a().b("_SX_DB_CUR");
      if (var7 != null) {
         for(int var8 = 0; var8 < var7.b(); ++var8) {
            com.aspose.cells.b.a.d.zh var9 = (com.aspose.cells.b.a.d.zh)var7.b(var8);
            var3.a();
            var4.a(var9, (zby)var3);
            var9.b(0L);
         }

      }
   }
}
